//
//  YGJDiscoverVC.m
//  ygj-customer-iOS
//
//  Created by apple on 16/11/10.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "YGJDiscoverVC.h"
#import "HXNetwork.h"
#import "YGJDiscoverCell.h"
#import "YGJDiscoverModel.h"
#import "UIImageView+WebCache.h"
#import "SVProgressHUD.h"


@interface YGJDiscoverVC ()

@end

@implementation YGJDiscoverVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self initData];
    
    [self initScroller];
    
    [self loadBanner];

}

-(void) initData {
    
    self.view.backgroundColor = [UIColor grayColor];
    
    _dataArray = [NSMutableArray array];
    
}

-(void) loadBanner{
    HXResponseManager *manager = [HXResponseManager manager];
    
    [SVProgressHUD show];
    //这里可以直接给 http:// .......
    [manager getWithUrl:@"http://cms.yueguanjia.com/api/app/category_list" success:^(NSURLSessionDataTask * _Nullable task, id  _Nullable responseObject) {
        
        NSLog(@"The response is --> %@",responseObject);
        
        for (NSDictionary * dic in [responseObject objectForKey:@"body"]) {
            YGJDiscoverModel * model = [[YGJDiscoverModel alloc] init];
            model.banner_image_name = [NSString stringWithFormat:@"http://%@",dic[@"smeta"][@"thumb"]];
            [_dataArray addObject:model];
        }
        [_tableView reloadData];
        [SVProgressHUD dismiss];
    } fail:^(NSURLSessionDataTask * _Nullable task, NSError * _Nullable error) {
        [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",error]];
    }];

}

-(void) initScroller {
    
    self.pwScrollerView = [[PWScrollView alloc]initWithFrame:CGRectMake(0, 60, [UIScreen  mainScreen].bounds.size.width, 200) ImagesArray:@[@"banner3"]];
    [self.pwScrollerView tapImageViewBlock:^(NSInteger tag) {
        
        NSLog(@"点击图片Block回调  %zd",tag);
        
        
    }];
    
    [self.view addSubview:self.pwScrollerView];
}

#pragma mark tableViewDelegate

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArray.count;
}
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString * cellId = @"cellId";
    YGJDiscoverCell * cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"YGJDiscoverCell" owner:nil options:nil] lastObject];
    }
    YGJDiscoverModel * model = _dataArray[indexPath.row];
    [cell.banner_img sd_setImageWithURL:[NSURL URLWithString:model.banner_image_name]];
 
    
    return cell;
}
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 135;

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
